//
//  HMStatusTableViewCell.swift
//  微博
//
//  Created by 郭鉴增 on 2016/12/10.
//  Copyright © 2016年 郭鉴增. All rights reserved.
//

import UIKit
//自定义微博cell
/*
   1.原创微博视图
   2.转发微博视图
   3.底部的ToolBar
 */

//cell子控件之间的间距
let StatusItemMargin: CGFloat = 10

class HMStatusTableViewCell: UITableViewCell {
    
    //记录toolBar顶部约束
    var toolBarTopContraint: Constraint?
    
    //所需要的视图模型(viewModel)
    var statusViewModel: HMStatusViewModel? {
        didSet{
            //给原创微博绑定数据
            originalView.statusViewModel = statusViewModel
            
            //添加约束之前,卸载上一次的约束
            toolBarTopContraint?.uninstall()
            //榆树更新完成以后需要保证toolBar之前没有添加顶部约束
            if statusViewModel?.status?.retweeted_status != nil {
                //表示有转发数据  toolBar的顶部等于转发微博的底部
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopContraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                //没有转发微博数据  toolBar的顶部等于原创微博的底部
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopContraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            retweetView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
        }
    }
    
    //MARK: - 懒加载
    //原创微博视图
    private lazy var originalView: HMStatusOriginalView = HMStatusOriginalView()
    //转发微博视图
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
    
    //toolBar
    private lazy var toolBar: HMStatusToolBar = HMStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        contentView.backgroundColor = RandomColor()
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
        }
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        
        toolBar.snp_makeConstraints { (make) in
            toolBarTopContraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(retweetView)
            make.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            //关键约束.让toolbar的底部等于contentview的底部
            make.bottom.equalTo(contentView)
        }
        
        
        
    }
    
    
}









